package utilidades.number;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.TextField;
import javafx.util.StringConverter;

/**
 * Fertonani oscar
 */
public class MonedaTextField extends TextField {

  
    private SimpleDoubleProperty number = new SimpleDoubleProperty();
   
    //Si quiero q no tenga puntos ni nada....
   // NumberFormat nf =  new DecimalFormat("###");
    
    //Si quiero q este separado por tres puntos
    //NumberFormat nf =  new DecimalFormat("###,###");
   
    
    MonedaComverter converter=new MonedaComverter();
   
   
   
   
    

    public final Double getNumber() {
        return number.get();
    }

    public final void setNumber(Float value) {
        number.set(value);
    }

    public SimpleDoubleProperty numberProperty() {
        return number;
    }

    public MonedaTextField() {

        
       super();
       
       /*
     // simbolo.setDecimalSeparator('-');
      simbolo.setGroupingSeparator('-');
      final  DecimalFormat nf =  new DecimalFormat("###,###",simbolo);
        */
      
       
       
       textProperty().bindBidirectional(number, converter);
       this.setText("");
       
       this.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> textProperty, String oldValue, String texto) {
                if(texto!=null & !texto.isEmpty()){
                    if(!texto.equals(oldValue)){
                    if(texto.matches("(\\$)?(-)?(\\d)?(\\d|\\.)*\\,?\\d*")){
                      //System.out.println("numero acutal "+number.get());
                    }else{
                     //   System.out.println("vuelvo al viejo "+oldValue);
                        setText(oldValue);
                    }
                    }
                }}});
       
       focusedProperty().addListener(new ChangeListener<Boolean>() {

            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if (!newValue.booleanValue()) {
                    setText(converter.toString(number.get()));
                }
            }
        });  
    }
    
    
    private class MonedaComverter  extends StringConverter{

        
        Locale localeAr = new Locale("es","ar");   
        NumberFormat npf =  NumberFormat.getCurrencyInstance(localeAr);
        
        @Override
        public String toString(Object t) {
           Double valor=(Double) t;
           return npf.format(valor);
        }

        @Override
        public Object fromString(String string) {
            if(string.isEmpty()){
                return null;
            }
            if(string.charAt(0)!='$'){
                 try {
                    return  npf.parse("$"+string);
                } catch (ParseException ex) {
                   // Logger.getLogger(MonedaTextField.class.getName()).log(Level.SEVERE, null, ex);
                }
        
            }else{
                 try {
                    return  npf.parse(string);
                   
                } catch (ParseException ex) {
                  //  Logger.getLogger(MonedaTextField.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            return null;
        }

    }
   
    
    
    
}
